using System.ComponentModel;
using EnergyInfo.DAL.LedaTableAdapters;

namespace EnergyInfo.BLL
{
    [DataObject]
    public class ObjectBLL
    {
        #region Adapter

        private ObjectTableAdapter adapter = null;

        protected ObjectTableAdapter Adapter
        {
            get
            {
                if (adapter == null)
                {
                    adapter = new ObjectTableAdapter();
                }

                return adapter;
            }
        }

        #endregion // List Adapter

        #region Adapter

        private ObjectListTableAdapter listAdapter = null;

        protected ObjectListTableAdapter ListAdapter
        {
            get
            {
                if (listAdapter == null)
                {
                    listAdapter = new ObjectListTableAdapter();
                }

                return listAdapter;
            }
        }

        #endregion // List Adapter

        #region List methods

        /// <summary>
        /// Gets the list of objects registered in system.
        /// 
        /// </summary>
        /// <returns></returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
        public DAL.Leda.ObjectDataTable GetObjects()
        {
            return Adapter.GetObjects();
        }

        /// <summary>
        /// Gets all information about object by object identifier.
        /// </summary>
        /// <param name="objectID"></param>
        /// <returns></returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public DAL.Leda.ObjectDataTable GetObjectByObjectID(int objectID)
        {
            return Adapter.GetObjectByObjectID(objectID);
        }

        /// <summary>
        /// Gets the list of objects registered in system.
        /// 
        /// </summary>
        /// <returns></returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
        public DAL.Leda.ObjectListDataTable GetObjectsList()
        {
            return ListAdapter.GetObjects();
        }

        #endregion // List methods

        #region Add Methods

        [DataObjectMethodAttribute(DataObjectMethodType.Insert, true)]
        public bool AddObject(string code, string name, string description, string address,
                              decimal? positionX, decimal? positionY,
                              string index, string region, string way
            )
        {
            // Create a new ProductRow instance
            DAL.Leda.ObjectDataTable objects = new DAL.Leda.ObjectDataTable();
            DAL.Leda.ObjectRow objectRow = objects.NewObjectRow();

            FillRow(objectRow, code, name, description,
                    positionX, positionY,
                    address, index, region, way);

            // Add the new object
            objects.AddObjectRow(objectRow);
            int rowsAffected = Adapter.Update(objects);

            // Return true if precisely one row was inserted,
            // otherwise false
            return rowsAffected == 1;
        }

        #endregion // Add Methods

        #region Update Functions

        [DataObjectMethodAttribute(DataObjectMethodType.Update, true)]
        public bool UpdateObject(int objectID,
                                 string code, string name, string description,
                                 decimal? positionX, decimal? positionY,
                                 string address, string index, string region, string way
            )
        {
            DAL.Leda.ObjectDataTable objects = Adapter.GetObjectByObjectID(objectID);
            if (objects.Count == 0)
            {
                // no matching record found, return false
                return false;
            }

            DAL.Leda.ObjectRow objectRow = objects[0];

            FillRow(objectRow, code, name, description,
                    positionX, positionY,
                    address, index, region, way);

            // Update the product record
            int rowsAffected = Adapter.Update(objectRow);

            // Return true if precisely one row was updated,
            // otherwise false
            return rowsAffected == 1;
        }

        #endregion /// Update Functions

        #region Delete Functions

        /// <summary>
        /// Deletes specified object from database.
        /// </summary>
        /// <param name="objectID"></param>
        /// <returns></returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Delete, true)]
        public bool DeleteObject(int objectID)
        {
            int rowsAffected = Adapter.DeleteObject(objectID);

            // Return true if precisely one row was deleted,
            // otherwise false
            return rowsAffected == 1;
        }

        #endregion // Delete Functions

        #region Helpers

        private static void FillRow(DAL.Leda.ObjectRow row,
                             string code, string name, string description,
                             decimal? positionX, decimal? positionY,
                             string address, string index, string region, string way
            )
        {
            // Code
            row.Code = code;
            // Name
            if (name == null)
            {
                row.SetNameNull();
            }
            else
            {
                row.Name = name;
            }
            // Description
            if (description == null)
            {
                row.SetDescriptionNull();
            }
            else
            {
                row.Description = description;
            }
            // Address
            if (address == null)
            {
                row.SetAddressNull();
            }
            else
            {
                row.Address = address;
            }
            // Position X
            if (positionX == null)
            {
                row.SetPositionXNull();
            }
            else
            {
                row.PositionX = positionX.Value;
            }
            // Position Y
            if (positionY == null)
            {
                row.SetPositionYNull();
            }
            else
            {
                row.PositionY = positionY.Value;
            }

            // Address
            if (address == null)
            {
                row.SetAddressNull();
            }
            else
            {
                row.Address = address;
            }
            // Index
            if (index == null)
            {
                row.SetIndexNull();
            }
            else
            {
                row.Index = index;
            }
            // Region
            if (region == null)
            {
                row.SetRegionNull();
            }
            else
            {
                row.Region = region;
            }
            // Way
            if (way == null)
            {
                row.SetWayNull();
            }
            else
            {
                row.Way = way;
            }
        }

        #endregion // Helpers
    }
}